home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Reader's Corner / Reader's Contibutions / David Hart / Source Code / Wallpaper 2.0 / Wallpaper.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-21  |  4.5 KB  |  188 lines  |  [TEXT/KAHL]

  1. /*
  2.     Filename    Wallpaper.c
  3.     Copyright © 1993 David Hart. All rights reserved.
  4.     Author        David Hart
  5.     Description    (Windows) Wallpaper module for After Dark
  6.     
  7.     Version      Date        Comments
  8.     2.00    23/03/93    Original Version for After Dark 2.0
  9.     2.01    26/03/93    To use the pict rect to get the width & height
  10. */
  11.  
  12. #include <QuickDraw.h>
  13. #include <Memory.h>
  14. #include <OSUtils.h>
  15. #include "GraphicsModule_Types.h"
  16.  
  17. #define PICTID        128        /* ID of first PICT */
  18. #define NUMPICTS    8        /* number of PICTS in resource */
  19.  
  20. /* Function prototypes */
  21. int        Rand(int range);
  22.  
  23. /* Specific graphics module data structures */
  24. typedef struct MyStorage
  25.     {
  26.     int    monitor;
  27.     int    h;
  28.     int    v;
  29.     int    width;
  30.     int    height;
  31.     int    pictNum;
  32.     } MyStorage, *MyStoragePtr, **MyStorageHandle;
  33.  
  34. /*
  35.     DoInitialize is the first function called from After Dark.
  36.     Memory for the structure is allocated and the variables are initialized.
  37.     The allocated memory is assigned to "storage"
  38.     and the function returns "noErr" if there are no problems.
  39. */
  40. OSErr    DoInitialize(Handle *storage, RgnHandle blankRgn, GMParamBlockPtr params)
  41.     {
  42.     MyStorageHandle myStorage;
  43.     int    pictNum;
  44.     
  45.     /* allocate handle to my storage */
  46.     myStorage = (MyStorageHandle)NewHandle(sizeof(MyStorage));
  47.     
  48.     if (!myStorage)
  49.         return MemError();
  50.     
  51.     MoveHHi(myStorage);
  52.     HLock(myStorage);                    /* Lock it down */
  53.     
  54.     *storage = (Handle)myStorage;        /* Assign myStorage to passed handle */
  55.  
  56.     (**myStorage).monitor = 0;
  57.     (**myStorage).h = 0;
  58.     (**myStorage).v = 0;
  59.     (**myStorage).pictNum = Rand(NUMPICTS) + PICTID;
  60.     HUnlock(myStorage);
  61.     return noErr;
  62.     }
  63.  
  64. /*  
  65.     DoBlank is the next function called.
  66.     It is used to simply blank the screen.
  67. */ 
  68. OSErr    DoBlank(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params)
  69.     {
  70.     FillRgn(blankRgn, params->qdGlobalsCopy->qdBlack);
  71.     return noErr;
  72.     }
  73.  
  74. /*
  75.     DoDrawFrame does almost all of the work.
  76. */
  77. OSErr    DoDrawFrame(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params)
  78.     {
  79.     OSErr err = noErr;
  80.     MyStoragePtr myStorage;                /* to hold dereferenced storage handle */
  81.     Rect    pictRect;
  82.     Rect    rect;
  83.     PicHandle    pict;
  84.     int    nextPict;
  85.     
  86.     /* Lock our storage down so we can dereference it once for faster access */
  87.     MoveHHi(storage);
  88.     HLock(storage);
  89.     myStorage = (MyStoragePtr)*storage;
  90.     
  91.     /* Get the PICT resource, draw it and replease it */
  92.     pict = GetPicture(myStorage->pictNum);
  93.     myStorage->width = (**pict).picFrame.right - (**pict).picFrame.left;
  94.     myStorage->height = (**pict).picFrame.bottom - (**pict).picFrame.top;
  95.     SetRect(&pictRect, myStorage->h, myStorage->v, myStorage->h+myStorage->width, myStorage->v+myStorage->height);
  96.     DrawPicture(pict, &pictRect);
  97.     ReleaseResource(pict);
  98.     
  99.     rect = params->monitors->monitorList[myStorage->monitor].bounds;
  100.     
  101.     /* Next vertical position */
  102.     myStorage->v += myStorage->height;
  103.     if (myStorage->v >= rect.bottom)
  104.         {
  105.         /* Off the bottom, reset to top and try next horizontal */
  106.         myStorage->v = rect.top;
  107.         
  108.         /* Next horizontal position */
  109.         myStorage->h += myStorage->width;
  110.         if (myStorage->h >= rect.right)
  111.             {
  112.             /* Off the right, reset to left and try next monitor */
  113.             myStorage->h = rect.left;
  114.             
  115.             /* Next monitor */
  116.             myStorage->monitor++;
  117.             if (myStorage->monitor >= params->monitors->monitorCount)
  118.                 {
  119.                 /* All monitors done, try another pict */
  120.                 myStorage->monitor = 0;
  121.                 
  122.                 /* Another pict at random */
  123.                 nextPict = Rand(NUMPICTS) + PICTID;
  124.                 if (nextPict == myStorage->pictNum)
  125.                     {
  126.                     /* Same as last one so use the 'next' one */
  127.                     nextPict++;
  128.                     if (nextPict >= NUMPICTS + PICTID)
  129.                         nextPict = PICTID;
  130.                     }
  131.                 myStorage->pictNum = nextPict;
  132.                 }
  133.             
  134.             /* Monitor bounds rect */
  135.             rect = params->monitors->monitorList[myStorage->monitor].bounds;
  136.             myStorage->h = rect.left;
  137.             myStorage->v = rect.top;
  138.             }
  139.         }
  140.     
  141.     HUnlock(storage);
  142.     return noErr;
  143.     }
  144.  
  145. /*
  146.     DoClose is called when the user quits the screen saver.
  147.     The memory is deallocated and the function returns "MemError"
  148.     which will tell After Dark if there was a problem
  149. */
  150. OSErr    DoClose(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params)
  151.     {
  152.     MyStorageHandle myStorage = (MyStorageHandle)storage;
  153.     int    pictNum;
  154.         
  155.     /* Deallocate our storage */
  156.     if (myStorage)
  157.         {
  158.         MoveHHi(myStorage);
  159.         HLock(myStorage);
  160.         
  161.         DisposHandle(storage);
  162.         }
  163.     
  164.     /* check for memory errors */
  165.     return MemError();
  166.     }
  167.  
  168. /*
  169.     DoSetUp is called if the user clicks on a button in the Control Panel.
  170. */
  171. OSErr    DoSetUp(RgnHandle blankRgn, short message, GMParamBlockPtr params)
  172.     {
  173.     return noErr;
  174.     }
  175.  
  176. /*
  177.     Rand returns value (0..range-1)
  178. */
  179. int    Rand(int range)
  180.     {
  181.     long    result = Random();
  182.     
  183.     if (result < 0)
  184.         result =- result;
  185.     
  186.     return ((result * range) / 32768);
  187.     }
  188.